package com.leetcode.p200;

import java.util.HashSet;
import java.util.Set;

public class Test202 {

    private Set<Integer> set = new HashSet<>();

    private int getNext(int n) {
        int totalSum = 0;
        while (n > 0) {
            int d = n % 10;
            n = n / 10;
            totalSum += d * d;
        }
        return totalSum;
    }

    public boolean isHappy(int n) {
        if (n == 1) {
            return true;
        }

        if (!set.add(n)) {
            return false;
        }

        return isHappy(getNext(n));
    }

    public static void main(String[] args) {
        Test202 test = new Test202();
//        System.out.println(test.isHappy(82));
        System.out.println(test.isHappy(81));
//        System.out.println(test.isHappy(1));
    }
}
